import { getIdentify } from "@/assets/directives/tools";
import { DrawerFormInlineType } from "./type";
export const treeDataOptions = ref<any>([]);
export const operateType = ref<any>("");
export const tableLoading = ref(false);
export const addLoading = ref<any>(false);
export const formInline = reactive<DrawerFormInlineType>({});
export const resetFormInline = (formState) => {
  const defaultFormInline = reactive<DrawerFormInlineType>({
    id: undefined,
    name: undefined,
    parent_id: undefined,
    http_path: undefined,
    http_method: "POST",
    is_menu: 1,
    sort: 0,
    slug: undefined,
    visible: 1,
    icon: "",
  });
  Object.keys(defaultFormInline).forEach((key) => {
    formState[key] = defaultFormInline[key];
  });
};
resetFormInline(formInline);
export const set_formInline = (type: string, formInline: any, data?: any) => {
  resetFormInline(formInline);
  if (type === "edit") {
    initObjAttr(formInline, data, "target");
    formInline.parent_id =
      +formInline.parent_id === 0 ? undefined : formInline.parent_id;
  }
};

export const drawer_disable_ok_button = computed(() => {
  if ([2].includes(formInline.is_menu as number)) {
    return (
      isEmptyType(formInline.is_menu) ||
      isEmptyType(formInline.name) ||
      isEmptyType(formInline.slug) ||
      isEmptyType(formInline.sort) ||
      isEmptyType(formInline.visible)
    );
  }
  if ([0, 1].includes(formInline.is_menu as number)) {
    return (
      isEmptyType(formInline.is_menu) ||
      isEmptyType(formInline.name) ||
      isEmptyType(formInline.http_method) ||
      isEmptyType(formInline.http_path) ||
      isEmptyType(formInline.sort) ||
      isEmptyType(formInline.visible)
    );
  }

  if ([2, 3].includes(formInline.is_menu as number)) {
    return (
      isEmptyType(formInline.is_menu) ||
      isEmptyType(formInline.name) ||
      isEmptyType(formInline.http_method) ||
      isEmptyType(formInline.http_path) ||
      isEmptyType(formInline.sort) ||
      isEmptyType(formInline.slug) ||
      isEmptyType(formInline.visible)
    );
  }
});

export function findParentNode(treeData, parentId) {
  for (let i = 0; i < treeData.length; i++) {
    const node = treeData[i];
    if (node.id === parentId) return node;
    else if (Array.isArray(node.children)) {
      const result = findParentNode(node.children, parentId);
      if (result !== null) return result;
    }
  }
  return null;
}

export const getSelfNodeIcon = (identify = "", treeData: any = []) => {
  identify = getIdentify(identify) || "";
  if (isEmptyType(identify)) return "";
  const routeStore = useRouteStore();
  treeData = routeStore.im_routes;
  const deelTreeData = (treeData, identify) => {
    for (let i = 0; i < treeData.length; i++) {
      const node = treeData[i];
      if (node.slug === identify) return node;
      else if (Array.isArray(node.children)) {
        const result = deelTreeData(node.children, identify);
        if (result !== null) return result;
      }
    }
    return null;
  };
  return deelTreeData(treeData, identify)?.icon ?? "";
};
